package dao

import (
	"fmt"
	"log"
	"time"
	"ztool/common"
)

// SDKUsers SDK授权
type SDKUsers struct {
	ID          uint       `json:"id"`                               //用户ID
	Detail      string     `json:"detail" form:"detail"`             //用户说明
	Token       string     `json:"token"`                            //token
	IP          string     `json:"ip" form:"ip"`                     //限定IP
	ExpiresDate *time.Time `json:"expires_date" form:"expires_date"` //过期时间
	CreatedAt   time.Time  `json:"created_at"`
}

// CreateSDKUser 创建用户
func CreateSDKUser(user *SDKUsers) {
	log.Println("创建SDK用户")
	db := common.GetDB()
	user.Token, _ = common.CreateApiToken(user.ExpiresDate, user.IP)

	if user.ExpiresDate.IsZero() {
		fmt.Println("日期为空")
		user.ExpiresDate = nil
	}

	db.Model(&SDKUsers{}).Create(&user)
}

// GetSDKUserList 查询SDK用户列表
func GetSDKUserList() *[]SDKUsers {
	db := common.GetDB()

	users := []SDKUsers{}

	result := db.Order("created_at desc").Find(&users)

	if nil != result.Error {
		return nil
	}

	return &users
}

// DelSDKUser 删除用户
func DelSDKUser(id *string) {
	db := common.GetDB()
	db.Delete(&SDKUsers{}, id)
}

////UpdateUserPwd 修改用户密码
//func UpdateUserPwd(pwd string, username string) {
//	db := common.GetDB()
//	npwd := CreateSHA256(pwd)
//	db.Model(&Users{}).Where("username = ?", username).Update("password", npwd)
//}
